#ifndef MY_TCPSERVER_H
#define MY_TCPSERVER_H
#include <QTcpServer>
#include <QMap>

struct Net_attr
{
    QString IP = "127.0.0.1";
    int port = 6666;
};

class my_Socket :public QObject
{
    Q_OBJECT
public:
    my_Socket(QTcpSocket *tcpSocket,int order);
    int send(QString buf);
    QTcpSocket *my_tcpSocket;
    int id;
    Net_attr netattr;

signals:
    void readyRead(int id);
    void disconnected(int id);
private slots:
//    void    onNewConnection();//QTcpServer的newConnection()信号
    void    onSocketStateChange(QAbstractSocket::SocketState socketState);
    void    onClientConnected(); //Client Socket connected
    void    onClientDisconnected();//Client Socket disconnected
    void    onSocketReadyRead();//读取socket传入的数据
};

class my_TCPServer :public QObject
{
    Q_OBJECT
public:
    my_TCPServer();
    bool open(QString IP,int port);
    int send(QString buf,int id);
    QTcpServer *tcpServer; //TCP服务器
    QMap<int,my_Socket *> Socketlist;
    int count=0;//当前连接设备数量
    void close();
signals:
    void relay_control(int model,int ch,int index);
    void connected(int id);
    void disconnected(int id);
    void readresult(QString buf,int id);
private:
    int order=0;//id计数

private slots:
    void    onNewConnection();//QTcpServer的newConnection()信号
    void    onSocketReadyRead(int id);//读取socket传入的数据
    void onClientDisconnected(int id);
    virtual void execute(QString buf,int id);
};

#endif // MY_TCPSERVER_H
